<?php
namespace application\admin\model;

class AuthGroupAccess extends Base
{
    // 主键设置
    protected $pk = 'uid';

    // 类型转换
    protected $type = [
        'uid'       => 'integer',
        'group_id'  => 'integer'
    ];

    public function getItem($user_id)
    {
        $result = false;
        if($user_id) {
            $result = $this->where('uid', $user_id)->find();
        }

        return $result;
    }

    /**
     * 添加用户到角色组
     * @param $params
     */
    public function setItem($user_id, $group_id)
    {
        $result = ['status'=>0, 'value'=>0, 'msg'=>'success'];

        if($user_id && $group_id)
        {
            $row = self::get($user_id);
            if($row) {
                if($this->save(['group_id'=>$group_id], ['uid'=>$user_id])){
                }else{
                    // 失败
                    $result['status'] = 1;
                    $result['msg'] = $this->getError();
                }
            }else{
                if($this->data(['uid'=>$user_id, 'group_id'=>$group_id])->save()){
                }else{
                    // 失败
                    $result['status'] = 1;
                    $result['msg'] = $this->getError();
                }
            }
        }

        return $result;
    }

}